package com.aaa.power.controller;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.Role;
import com.aaa.power.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2020-11-10 13:52:29
 */
@RestController
@RequestMapping("role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        if(id==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }

        return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg()
                ,this.roleService.queryById(id));
    }

    /**
     * 分页加各种条件查询
     * @param pageNo
     * @param pageSize
     * @param role
     * @return
     */
    @GetMapping("queryPageByParam")
    Result queryPageByParam(Integer pageNo, Integer pageSize, Role role){
        return this.roleService.queryPageByParam(pageNo,pageSize,role);
    }

    /**
     *
     * @param role
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Role role){
        return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg()
                ,this.roleService.insert(role));
    }

    /**
     * 根据角色查询该角色拥有的所有权限id
     * @param roleId
     * @return
     */
    @GetMapping("listMenuByRoleId")
    public List<Integer> listMenuByRoleId(int roleId){
        return roleService.listMenuByRoleId(roleId);
    };

    /**
     * 添加角色权限关联数据
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("addRoleAndMenu")
    public Object addRoleAndMenu(Integer roleId,String menuIds){
        return roleService.addRoleAndMenu(roleId,menuIds);
    };

    /**
     * 删除
     * @param roleId
     * @return
     */
    @DeleteMapping("delete")
    public Object delete(Integer roleId){
        return roleService.delete(roleId);
    };

    /**
     * 角色更新
     * @param role
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody  Role role){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.roleService.update(role));
    }



}